﻿using ClewareWrapper.Invoke;

namespace ClewareWrapper
{
    public class SimpleTrafficLight
    {
        private Device _device;
        
        public SimpleTrafficLight(Device device)
        {
            _device = device;
        }

        public bool Red
        {
            get { return GetSwitch(SwitchId.SWITCH_0); }
            set { SetSwitch(SwitchId.SWITCH_0, value); }
        }

        public bool Orange
        {
            get { return GetSwitch(SwitchId.SWITCH_1); }
            set { SetSwitch(SwitchId.SWITCH_1, value); }
        }

        public bool Green
        {
            get { return GetSwitch(SwitchId.SWITCH_2); }
            set { SetSwitch(SwitchId.SWITCH_2, value); }
        }

        public void SetRedOnly()
        {
            SetExclusiveSwitch(SwitchId.SWITCH_0);
        }

        public void SetOrangeOnly()
        {
            SetExclusiveSwitch(SwitchId.SWITCH_1);
        }

        public void SetGreenOnly()
        {
            SetExclusiveSwitch(SwitchId.SWITCH_2);
        }

        public void SwitchOff()
        {
            foreach (var i in new[] { SwitchId.SWITCH_0, SwitchId.SWITCH_1, SwitchId.SWITCH_2 })
            {
                SetSwitch(i, false);
            }
        }

        private void SetExclusiveSwitch(SwitchId fcwswitch)
        {
            foreach (var i in new[]{ SwitchId.SWITCH_0, SwitchId.SWITCH_1, SwitchId.SWITCH_2})
            {
                SetSwitch(i, i == fcwswitch);
            }
        }

        private void SetSwitch(SwitchId fcwswitch, bool on)
        {
            _device.SetSwitch(fcwswitch, on ? 1 : 0);
        }

        private bool GetSwitch(SwitchId fcwswitch)
        {
            return _device.GetSwitch(fcwswitch) == 1;
        }
    }
}
